<?php
namespace Admin\Controller;
use Think\Controller;

/**
* 项目控制器
*/
class ProjectController extends AdminBasicController{
	
	public $Project = '';
	public $Member = '';

	public function _initialize(){
		$this->checkLogin();
		$this->Project = D('Project');
		$this->Member = D('Member');
	}


	public function selectProject(){
		$where=array();
		if(isset($_POST['p_name'])){
			$where['p_name'] = array('like','%'.$_POST['p_name'].'%');
		}
		$project_list = $this->Project->selectProject($where,'ctime desc',10);
		$this->assign('project_list',$project_list['list']);
        $this->assign('page',$project_list['page']);
        $this->display('selectProject');
	}


	/**
	 * 添加项目
	 */
	public function addProject(){
		if(empty($_POST)){
			$where['m_type'] = '2';
			$member_list = $this->Member->selectMember($where,'','');
			$time = time();
            $this->assign('time',$time);
			$this->assign('member_list',$member_list);
			$this->display('Project/addProject');
		}else{
			if($data=$this->Project->create()){
				$where['p_name'] = $_POST['p_name'];
				$pname = $this->Project->findProject($where);
				if($_FILES['p_logo']['name']){
					$log_pic= $this->uploadImg("Project",'p_logo');
					$data['p_logo'] = $log_pic;
				}
				if($pname){
					$this->error('该项目已经存在！');
				}
				$data['ctime'] = time();
				if($this->Project->addProject($data)){
					$this->success('添加成功',U('Project/selectProject'));
				}else{
					$this->success("添加失败");
				}
			}else{
				$this->error($this->Project->getError());	
			}
		}
	}

	/**
	 * 编辑模块
	 */
	public function editProject(){
		if(empty($_POST)){
			$where['m_type'] = '2';
			$member_list = $this->Member->selectMember($where,'','');
			$where1['p_id'] = I('get.p_id');
			$project_find = $this->Project->findProject($where1);
			$time = time();
            $this->assign('time',$time);
			$this->assign('project_find',$project_find);
			$this->assign('member_list',$member_list);
			$this->display('Project/editProject');
		}else{
			if($data=$this->Project->create()){
				if($_FILES['p_logo']['name']){
					$log_pic= $this->uploadImg("Project",'p_logo');
					$data['p_logo'] = $log_pic;
				}
				if($this->Project->editProject(array('p_id'=>$_POST['p_id']),$data)){
					$this->success('编辑成功',U('Project/selectProject'));
				}else{
					$this->success('编辑失败');
				}
			}else{
				$this->error($this->Project->getError());	
			}
		}
	}

	public function deleteProject(){
		$this->checkAuth('Project','deleteProject');
		if(empty($_REQUEST['p_id'])){
			$this->error('您未选择任何操作对象');
		}
		$where['p_id'] = array('IN',I('request.p_id'));
		$data['status'] = 9;
		$data['utime'] = time();
		$id = $this->Project->editProject($where,$data);
		if($id){
			$this->success('删除操作成功');
		}else{
			$this->error('删除操作失败');
		}
	}

    /**
     * 导出数据
     */
    public function exportExcel(){
        header("Content-type: text/html; charset=utf-8");
        //实例化对象
        $util_obj = D('Util','Service');
        $Project = D('Project');
        $Module = D('Module');
        $Function = D('Function');
        $where_pro['p_id'] = $_POST['p_id'];
        $project_info = $Project->where($where_pro)->find();

        //获取项目下模块列表
        $where_mod['p_id'] = $_POST['p_id'];
        $model_list = $Module->where($where_mod)->select();
        $bbb = array();
        foreach($model_list as $k=>$v){
            //获取模块下方法列表
            $where_fun['mo_id'] = $v['mo_id'];
            $model_list[$k]['function_list'] = $Function->where($where_fun)->select();
            foreach($model_list[$k]['function_list'] as $kk=>$vv){
                //整合方法数据
                //获取方法所属项目名称
                $model_list[$k]['function_list'][$kk]['p_name'] = $project_info['p_name'];
                //获取方法所属模块名称
                $model_list[$k]['function_list'][$kk]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                //拆分整理方法参数
                $model_list[$k]['function_list'][$kk]['f_pa_arr'] = unserialize($vv['f_pa']);
                $model_list[$k]['function_list'][$kk]['f_name'] = $vv['f_name'];
                if(is_array($model_list[$k]['function_list'][$kk]['f_pa_arr'])){
                    foreach($model_list[$k]['function_list'][$kk]['f_pa_arr'] as $kkk=>$vvv){
                        //获取参数所属项目名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_name'] = $project_info['p_name'];
                        //获取参数所属模块名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                        //获取参数所属方法名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_zh_name'] = $vv['f_zh_name'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_url'] = $vv['f_url'];

                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_type'] = $vvv['p_type'] ? "图片" : "文本";
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['pa_name'] = $vvv['p_name'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_desc'] = $vvv['p_desc'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_is_null'] = $vvv['p_is_null'] ? "是" : "否";

                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_type'] = $vv['f_type'] ? "POST": "GET";
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_success'] = $vv['f_success'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_error'] = $vv['f_error'];


                        $bbb[] = $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk];

                        //dump($model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]);

                    }
                }else{
                    //获取参数所属项目名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_name'] = $project_info['p_name'];
                    //获取参数所属模块名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                    //获取参数所属方法名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_zh_name'] = $vv['f_zh_name'];
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_url'] = $vv['f_url'];

                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_type'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['pa_name'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_desc'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_is_null'] = "无参数";

                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_type'] = $vv['f_type'] ? "POST": "GET";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_success'] = $vv['f_success'];
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_error'] = $vv['f_error'];
                    $bbb[] = $model_list[$k]['function_list'][$kk]['f_pa_arr'][0];
                }
            }
        }
        //字段数据
        $fields_data = I('post.fields_data');
        //执行导出函数
        $util_obj->exportExcel($fields_data,$bbb,$project_info['p_name'].'API');
    }


}

?>